home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Add-Ons / After Dark / Joe Judge / CyberWeb ƒ / module.c < prev   
Encoding:
C/C++ Source or Header  |  1994-07-18  |  6.2 KB  |  243 lines  |  [TEXT/KAHL]

  1.  
  2.  
  3. #include "GraphicsModule_Types.h"
  4. #include "Sounds.h"
  5.  
  6. #include <Picker.h>
  7. #include <Palettes.h>
  8.  
  9.  
  10. // these are the functs that need defined ...
  11. OSErr DoInitialize(Handle *storage, RgnHandle blankRgn, GMParamBlockPtr params);
  12. OSErr DoClose(Handle storage, RgnHandle blankRgn, GMParamBlockPtr params);
  13. OSErr DoBlank(Handle storage, RgnHandle blankRgn, GMParamBlockPtr params);
  14. OSErr DoDrawFrame(Handle storage, RgnHandle blankRgn, GMParamBlockPtr params);
  15. OSErr DoSetUp(RgnHandle blankRgn, short message, GMParamBlockPtr params);
  16. // these must be defined also
  17. OSErr DoSelected(RgnHandle blankRgn, short message, GMParamBlockPtr params);
  18. OSErr DoAboutBox(RgnHandle blankRgn, short message, GMParamBlockPtr params);
  19.  
  20.  
  21. struct foo {
  22.     double a,b;    
  23.     struct foo *next;
  24. };
  25.  
  26. //#define MAXX 480
  27. //#define MAXY 300
  28. #define MAXM 150        //400
  29.  
  30. #define START_SATURATION    32767            //8192            //4096
  31.  
  32. Boolean gDoColor = FALSE;
  33.  
  34.  
  35. int currentMonitor;
  36. void StepColor(SmallFract *theSaturation, SmallFract *theHue, 
  37.     short theSaturationDelta, short theDelta);
  38. unsigned short RangedRdm( unsigned short min, unsigned short max );
  39. unsigned short random(void);
  40.  
  41. //////////////////////////////////////////////////////////////////////////////////////
  42. // this is the first funct called by AD ... we need to allocate and initialize here
  43. OSErr
  44. DoInitialize(Handle *storage, RgnHandle blankRgn, GMParamBlockPtr params) {
  45. Boolean HasColorQD(void);
  46.  
  47.     if (HasColorQD())
  48.         gDoColor = TRUE;
  49.     currentMonitor = 0;
  50.     return noErr;
  51. }
  52.  
  53. //////////////////////////////////////////////////////////////////////////////////////
  54. // the screen saver has been awakened! time to ditch the storage and wave goodbye
  55. OSErr 
  56. DoClose(Handle storage, RgnHandle blankRgn, GMParamBlockPtr params) {
  57.  
  58.     return noErr;
  59. }
  60.  
  61.  
  62.  
  63. //////////////////////////////////////////////////////////////////////////////////////
  64. // make the screen go black
  65. OSErr
  66. DoBlank(Handle storage, RgnHandle blankRgn, GMParamBlockPtr params) {
  67.  
  68.     // darken the screen ...
  69.     FillRgn(blankRgn, params->qdGlobalsCopy->qdBlack);
  70.     return noErr;
  71.  
  72. }
  73.  
  74. //////////////////////////////////////////////////////////////////////////////////////
  75. // this is the workhorse routine. It does the continual screen work to make
  76. // this screen saver what it is.
  77. OSErr 
  78. DoDrawFrame(Handle storage, RgnHandle blankRgn, GMParamBlockPtr params) 
  79. {
  80.     Rect linesRect;
  81.     short number, x, n;
  82.     struct foo *top, *p, *p2;
  83.     double mx, my, nx, ny;
  84.     SmallFract theSaturation, theHue;
  85.     short theSaturationDelta;
  86.     short stepAmount;
  87.     short maxVertices;
  88.     
  89.     
  90.     BackColor( blackColor);
  91.     ForeColor( whiteColor);
  92.     
  93.     linesRect = params->monitors->monitorList[currentMonitor].bounds;
  94.         
  95.     // every (user-settable) so often, erase the on-screen mess
  96.     if (RangedRdm(0, 100) >= params->controlValues[1] )
  97.         EraseRect( &linesRect);
  98.     
  99.     //was: number = (random() % 3) + 2;
  100.     //number = (random() % 50) + 2;
  101.     maxVertices = 4 + (params->controlValues[3] / 10);
  102.     number = RangedRdm(2, maxVertices);
  103.     x = 0;
  104.     
  105.     top = p = (struct foo *)NewPtr( sizeof(struct foo));
  106.     while (x++ != number) {
  107.         p->next = (struct foo *)NewPtr( sizeof(struct foo));
  108.         if (p->next == NULL) {
  109.             BlockMove( "\pNewPtr failed - oops lost some memory, too!",
  110.                     ¶ms->errorMessage[0], 15 );
  111.             return ModuleError;
  112.         }
  113.         p = p->next;
  114.     }
  115.     p->next = top;
  116.     p = top;
  117.  
  118. //////////////////////////////////////
  119.     // initial values for these
  120.     mx = 0;
  121.     my = 0;
  122.     nx = 9999999.0;        //• Large numbers for finding minimum value.
  123.     ny = 9999999.0;
  124.     
  125.     do {
  126.         p->a =  (double) random ();
  127.         if  (p->a > mx) mx = p->a;
  128.         if  (p->a < nx) nx = p->a;
  129.         
  130.         p->b =  (double) random ();
  131.         if  (p->b > my) my = p->b;
  132.         if  (p->b < ny) ny = p->b;
  133.         
  134.         p = p->next;
  135.     } while  (p != top);
  136.     
  137.     do {        
  138.         p->a =  ((p->a - nx) * (linesRect.right-linesRect.left)) /  (mx - nx);
  139.         p->b =  ((p->b - ny) * (linesRect.bottom-linesRect.top)) /  (my - ny);
  140.         p = p->next;    
  141.     } while  (p != top);
  142.  
  143.     MoveTo ( linesRect.left + (int) p->a, linesRect.top + (int) p->b);
  144.     if (gDoColor && 
  145.             (params->monitors->monitorList[currentMonitor].curDepth > 2)  ) {
  146.         theSaturation = START_SATURATION;
  147.         theHue = RangedRdm(0, 65535);
  148.     }
  149.  
  150.  
  151.     // I like this better (scalable)
  152.     // it turns out that 1/100 of 1000 is 10
  153.     // so ... 
  154.     theSaturationDelta = 10 * params->controlValues[0];
  155.     
  156.     //theSaturationDelta = (params->controlValues[0] / 10) * 100;    
  157.     // we go from { 0, 10, 20, ... 500 ... 1000 }
  158.     
  159.     n = 0;
  160.     while  (n++ < (MAXM * number) ) {
  161. //#define AMT 20
  162. #if 0
  163.         p->a =  (p->a * AMT + p->next->a) / (AMT+1);
  164.         p->b =  (p->b * AMT + p->next->b) / (AMT+1);
  165. #else
  166.         stepAmount = 1 + (params->controlValues[2] / 5);
  167.         
  168.         p->a =  (p->a * stepAmount + p->next->a) / (stepAmount+1);
  169.         p->b =  (p->b * stepAmount + p->next->b) / (stepAmount+1);
  170. #endif
  171.         if (gDoColor && 
  172.                 (params->monitors->monitorList[currentMonitor].curDepth > 2)  ) {
  173.             StepColor(&theSaturation, &theHue, theSaturationDelta, (MAXM*number) );        
  174.         }        
  175.         LineTo ( linesRect.left + (int)p->a, linesRect.top + (int)p->b);
  176.         p = p->next;
  177.     }
  178.     
  179.     p = top;    //• Clean up the circular linked list.
  180.     do 
  181.     {
  182.         p =  (p2 = p)->next;
  183.         DisposPtr ( (Ptr)p2);
  184.     } while  (p != top);    
  185. //////////////////////////////////////
  186.  
  187.     // next time around, use the next monitor
  188.     currentMonitor++;
  189.     if (currentMonitor >= params->monitors->monitorCount)
  190.         currentMonitor = 0;
  191.  
  192.     return noErr;
  193. }
  194.  
  195. //////////////////////////////////////////////////////////////////////////////////////
  196. // this is called when they click on something in the control panel
  197. OSErr 
  198. DoSetUp(RgnHandle blankRgn, short message, GMParamBlockPtr params) {
  199.     // button got pushed?? 
  200.     return noErr;
  201. }
  202.  
  203.  
  204.  
  205. OSErr DoSelected(RgnHandle blankRgn, short message, GMParamBlockPtr params) {
  206.     return noErr;
  207. }
  208.  
  209.  
  210.  
  211. OSErr DoAboutBox(RgnHandle blankRgn, short message, GMParamBlockPtr params) {
  212.     return noErr;
  213. }
  214.  
  215.  
  216. void
  217. StepColor(
  218.     SmallFract *theSaturation, SmallFract *theHue, 
  219.     short theSaturationDelta, short theDelta) {
  220. RGBColor rColor;
  221. HSVColor hColor;
  222.  
  223.  
  224.     *theSaturation +=  ( (65535-START_SATURATION) / theDelta);
  225.     
  226.     *theHue += theSaturationDelta;    //65535/theDelta;
  227.     if (*theHue >= 65535)
  228.         *theHue %= 65535;
  229.  
  230.     // any hue/color
  231.     hColor.hue = *theHue;
  232.         
  233.     // lets just choose the brightest colors
  234.     hColor.saturation = *theSaturation;
  235.  
  236.     // any only the brighter half values of it
  237.     hColor.value = RangedRdm( 49150, 65535);
  238.     
  239.     HSV2RGB( &hColor, &rColor);
  240.     RGBForeColor( &rColor);            // pmForeColor() if you want to animate
  241. }
  242.  
  243.